import React, { Component } from "react";
import { connect } from "react-redux";
import { Toast } from "antd-mobile";
import "./index.scss";
import MainLayout from "@component/mainLayout/mainLayout";
import {
	getDeviceInfoDatas,
	getAppPlatformsDatas
} from "@redux/Action/nativeDatas/nativeDatas";
import { InterfaceG } from "@config/Config";
import yhTools from "@tools/yh";
import FrontImg from "@images/imagesXHD/xhd-back-bg.png";
import BackImg from "@images/imagesXHD/xhd-front-bg.png";
class XhdUpIdCard extends Component {
	constructor(props) {
		super(props);
		this.state = {
			name: ""
		};
	}
	componentDidMount() {
		let state = this.props.location.state;
		this.setState({
			name: state.name
		});
	}
	//名字掩码
	noPassByName = str => {
		if (null != str && str != undefined) {
			if (str.length <= 3) {
				return "*" + str.substring(1, str.length);
			} else if (str.length > 3 && str.length <= 6) {
				return "**" + str.substring(2, str.length);
			} else if (str.length > 6) {
				return str.substring(0, 2) + "****" + str.substring(6, str.length);
			}
		} else {
			return "";
		}
	};
	//选择图片
	chooseImage(index) {
		//打开原生获取图片
		let imgPicker = {
			type: "3", //相机和相册
			imageName:
				this.props.getAppPlatformsDatas.mobile + "_card_" + index + ".png",
			imageSize: 100 * 1024 + "", // 100kb,自定义
			thumbnailSize: 100 * 1024 + "" // 100kb,自定义
		};

		yhTools.chooseImage(imgPicker).then(
			resp => {
				console.log(JSON.stringify(resp.data.base64), "------------------");
				switch (index) {
					case 0:
						//上传身份证人像图片
						this.updateImage(2, resp.data.path);
						this.setState({
							imageFront: resp.data.base64
						});
						break;
					case 1:
						//上传身份证国徽图片
						this.updateImage(4, resp.data.path);
						this.setState({
							imageBack: resp.data.base64
						});
						break;
					default:
						break;
				}
			},
			() => {}
		);
	}
	//上传身份证照片
	updateImage(index, path) {
		let imageData = {
			name: "CardImg.jpeg",
			path: path,
			params: {
				fileType: index, //2正面，4反面，5头像
				channelCode: "YHB", //渠道编号,
				version: "1.0",
				productNum: "802"
			}
		};
		let _that = this;
		let jsonDataArr = [imageData];
		let objs = {
			url: InterfaceG.xhdUploadIdCardOcrForHt,
			imageArray: jsonDataArr
		};
		Toast.loading("加载中...", 100, () => {});
		yhTools.request(objs).then(resp => {
			Toast.hide();
			console.log(resp);
			if (resp.code == "0") {
				Toast.info("上传成功", 2);
				if (index == "2") {
					_that.setState({
						chooseImageFront: "1", //选择人像面图片上传成功
						username: resp.data.userName, //姓名
						idno: resp.data.idNo, //身份证号
						sex: resp.data.sex, //性别
						address: resp.data.address, //地址
						nation: resp.data.nation, //民族
						birth: resp.data.birth, //出生日期
						frontImageUrl: resp.data.frontImageUrl,
						frontImage: resp.data.frontImage
					});
				} else {
					_that.setState({
						chooseImageBack: "1", //选择国徽面图片上传成功
						organs: resp.data.organs, //签发机关
						signStartDate: resp.data.signStartDate, //签发日期
						validDate: resp.data.validDate, //有效期
						backImageUrl: resp.data.backImageUrl,
						backImage: resp.data.backImage
					});
				}
			} else {
				if (index == "2") {
					_that.setState({
						chooseImageFront: "-1", //选择人像面图片上传失败
						idno: "", //身份证号
						sex: "", //性别
						address: "", //地址
						nation: "", //民族
						birth: "" //出生日期
					});
				} else {
					_that.setState({
						chooseImageBack: "-1", //选择国徽面图片上传失败
						organs: "", //签发机关
						signStartDate: "", //签发日期
						validDate: "" //有效期
					});
				}
				Toast.info(resp.message, 2);
			}
		});
	}
	onSubmit = () => {
		const {
			username,
			idno,
			sex,
			address,
			nation,
			birth,
			organs,
			signStartDate,
			validDate,
			professionValue,
			professionCode,
			frontImage,
			frontImageUrl,
			backImage,
			backImageUrl
		} = this.state;
		let dataJSON = {
			url: InterfaceG.saveIdInfo,
			params: {
				channelCode: "YHB", //渠道编号,
				applyNo: this.props.getAppPlatformsDatas.applyNo, //申请流水号
				lnid: this.props.getAppPlatformsDatas.lnid, //产品id
				productNum: "802",
				username: username, //姓名
				idno: idno, //身份证号
				sex: sex, //性别
				address: address, //地址
				nation: nation, //民族
				birth: birth, //出生日期
				organs: organs, //签发机关
				signStartDate: signStartDate, //签发日期
				validDate: validDate, //有效期
				professionValue: professionValue, //职业
				professionCode: professionCode,
				frontImage: frontImage, //人脸照片
				backImage: backImage, //国徽照片
				frontImgUrl: frontImageUrl,
				backImgUrl: backImageUrl,
				version: "1.0"
			},
			_alertShow: false
		};
		Toast.loading("加载中...", 100, () => {});
		yhTools.request(dataJSON).then(resp => {
			Toast.hide();
			if (resp.code == "0") {
				this.props.history.push({
					pathname: "xhdLoan",
					state: this.props.location.state
				});
			} else {
				Toast.info(resp.message, 2);
			}
		});
	};
	render() {
		const { imageFront, imageBack, name } = this.state;
		let _pageParams = {
			header: {
				title: "上传身份影像"
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};
		console.log(this.props);
		return (
			<MainLayout pageParams={_pageParams}>
				<div className="xhd-up-idcard">
					<p className="title">
						上传“<i>{this.noPassByName(name)}</i>”本人身份证照片
					</p>
					<div className="infor">拍照/上传您的身份证</div>
					<div className="upload" onClick={this.chooseImage.bind(this, 0)}>
						<img src={imageFront ? imageFront : FrontImg} />
					</div>
					<div className="upload" onClick={this.chooseImage.bind(this, 1)}>
						<img src={imageBack ? imageBack : BackImg} />
					</div>
					<div className="btn" onClick={this.onSubmit}>
						确认并提交
					</div>
				</div>
			</MainLayout>
		);
	}
}
function mapStateToProps(state) {
	return state;
}

function mapDispatchToProps(dispatch) {
	return {
		_getDeviceInfoDatas: id => {
			//获取设备信息
			dispatch(getDeviceInfoDatas(id));
		},
		getAppPlatformsDatas: options => {
			//获取设备信息
			dispatch(getAppPlatformsDatas(options));
		}
	};
}

const XhdUpIdCardPage = connect(
	mapStateToProps,
	mapDispatchToProps
)(XhdUpIdCard);
export default XhdUpIdCardPage;
